package com.fancy;

/**
 * @author chaigq
 * @version 1.0
 * @date 2021/5/23 11:15 下午
 */
public class VirtualOnlineSystem {

    // 这里模拟的是频繁触发MinorGC 和FullGC的情况，1s内触发一次MinorGC，2-3s触发FullGC

    public static void main(String[] args) throws Exception {
        Thread.sleep(30000);
        while (true){
            // 每秒钟都会执⾏⼀次loadData()⽅法
            loadData();
        }
    }

    private static void loadData() throws Exception {
        byte[] data = null;
        // data分配4个10MB的数组,data之后变成垃圾
        for (int i = 0; i < 4; i++) {
            data = new byte[10 * 1024 * 1024];
        }
        data = null;
        // 2两个10MB的数组是被变量引⽤必须存活的，此时Eden区已占用40M + data1和data2 共60M
        byte[] data1 = new byte[10 * 1024 * 1024];
        byte[] data2 = new byte[10 * 1024 * 1024];

        byte[] data3 = new byte[10 * 1024 * 1024];
        data3 = new byte[10 * 1024 * 1024];
        Thread.sleep(10000);
    }

    // TODO 调整堆内存大小的JVM参数
    //-XX:NewSize=209715200 -XX:MaxNewSize=209715200 -XX:InitialHeapSize=314572800 -XX:MaxHeapSize=314572800 -
    //XX:SurvivorRatio=2  -XX:MaxTenuringThreshold=15 -XX:PretenureSizeThreshold=20971520 -XX:+UseParNewGC -
    //XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log
}
